package com.hotchpotch.activity;

import android.os.Handler;
import android.os.Message;
import android.widget.TextView;

import com.hotchpotch.R;
import com.hotchpotch.base.AppConstant;
import com.hotchpotch.base.BaseActivity;
import com.hotchpotch.bean.RetrofitTestBean;
import com.hotchpotch.bean.TestBean1;
import com.hotchpotch.retrofit.ResultCallback;
import com.hotchpotch.retrofit.RetrofitManager;
import com.hotchpotch.retrofit.RetrofitService;
import com.hotchpotch.util.Logg;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.scalars.ScalarsConverterFactory;

/**
 * Created by Administrator on 2017/10/17 0017.
 */

public class RetrofitActivity extends BaseActivity {
    private static final String TAG = "Retrofit测试类";
    private TextView textView;
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
        }
    };

    @Override
    protected int getLayoutId() {
        return R.layout.activity_retrofit;
    }

    @Override
    protected void initView() {
        textView = (TextView) findViewById(R.id.retrofit_text);
    }

    @Override
    protected void initEvents() {

    }

    @Override
    protected void initData() {
        getData();
    }

    private void getData() {
        showLoading();
        RetrofitManager.doGet("check_update.php", new ResultCallback<RetrofitTestBean>() {
            @Override
            public void onResponse(RetrofitTestBean bean) {
                super.onResponse(bean);
                textView.setText(bean.getDownurl());
            }
        });

        RequestBody requestBody = new FormBody.Builder()
                .add("action", "get_show_bank")
                .add("user_id", "2")
                .build();
        RetrofitManager.doPost("authentication/user_authentication.php", requestBody, new ResultCallback<TestBean1>(){
            @Override
            public void onResponse(TestBean1 result) {
                super.onResponse(result);
                Logg.e(TAG, "result = " + result);
            }

            @Override
            public void onError(Call<TestBean1> call, Throwable e) {
                super.onError(call, e);
            }
        });
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                cancelLoading();
            }
        }, 3000);
    }

    /**
     * Retrofit获取数据的普通写法
     */
    private void getData2() {
        //创建Retrofit对象
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(AppConstant.test_url2)//设置请求地址
//                .addConverterFactory(GsonConverterFactory.create())//设置使用Gson解析(记得加入依赖)
                .addConverterFactory(ScalarsConverterFactory.create())//设置String转化器
                .build();
        //创建 网络请求接口 的实例
        RetrofitService request = retrofit.create(RetrofitService.class);
        //对 发送请求 进行封装
        Call<ResponseBody> call = request.doGet("check_update.php");
        Logg.e(TAG, "执行请求");
        //发送网络请求(异步)
        call.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                try {
                    Logg.e(TAG, "连接成功");
                    Logg.e(TAG, "数据 = " + response.body().string());
                } catch (IOException e) {
                    e.printStackTrace();
                }
//                RetrofitTestBean bean = JsonUtil.parseJson(response.body(), RetrofitTestBean.class);
//                Logg.e(TAG, "解析数据 = " + bean);
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                Logg.e(TAG, "连接失败");
            }
        });
    }

    private void upload() {
        List<String> pathList = new ArrayList<>();//此处是伪代码，获取多张待上传图片的地址列表
        RetrofitManager.uploadImgs("", pathList, new ResultCallback<String>() {
            @Override
            public void onResponse(String result) {
                super.onResponse(result);
            }
        });
    }
}
